package com.jing.weibo.controller.pb;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jing.system.comm.BaseController;
import com.jing.system.comm.contants.DataCons;
import com.jing.weibo.pojo.pb.PbType;
import com.jing.weibo.pojo.user.UserInfo;
import com.jing.weibo.service.pb.PbTypeService;

/**
 * 泡吧分区的Controller
 * @author jing.yue
 * @version 2012/09/29 1.0.0
 */
@Controller
public class PbTypeController extends BaseController {

	private static final Logger logger = Logger.getLogger(PbTypeController.class);

	@Autowired
	private PbTypeService pbTypeService;

	/**
	 * 添加泡吧分区
	 * @return
	 */
	@RequestMapping(value = "/pbType/json/addPbType")
	@ResponseBody
	public ModelMap addPbType(HttpServletRequest request, PbType pbType) {
		String result = null;
		UserInfo userInfo = getSessionUser(request);
		//不为管理员
		if(userInfo.getIsadmin().intValue() != 1) {
			logger.error(userInfo.getNickname() + ": 您不是管理员!");
			result = ERROR_ADMIN;
		}
		else {
			try {
				pbType.setAdduser(userInfo.getUsername());
				pbTypeService.savePbType(pbType);
				result = SUCCESS;
				//同步数据
				DataCons.initPbTypeMap(null, request.getSession().getServletContext());
			} catch (Exception e) {
				logger.error(userInfo.getNickname() + ": 添加泡吧分区异常!", e);
				result = ERROR;
			}
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		return modelMap;
	}

	/**
	 * 跳转到泡吧分区管理页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/toPbTypeManager")
	public String toPbTypeManager(HttpServletRequest request, ModelMap modelMap) {
		UserInfo userInfo = getSessionUser(request);
		//不为管理员
		if(userInfo.getIsadmin().intValue() != 1) {
			logger.error(userInfo.getNickname() + ": 您不是管理员!");
			return ERROR_ADMIN;
		}
		try {
			//获取泡吧分区
			List<PbType> pbTypeList = pbTypeService.findAllPbType();
			modelMap.put("pbTypeList", pbTypeList);
		} catch (Exception e) {
			logger.error("获取泡吧分区异常!", e);
		}
		return "admin/pb/pbtype_manager";
	}

	/**
	 * 查找泡吧分区
	 * @return
	 */
	@RequestMapping(value = "/pbType/json/findPbType")
	@ResponseBody
	public ModelMap findPbType(HttpServletRequest request) {
		String result = null;
		List<PbType> pbTypeList = null;
		try {
			//获取泡吧分区
			pbTypeList = pbTypeService.findAllDetailPbType();
			result = SUCCESS;
		} catch (Exception e) {
			logger.error("获取泡吧分区异常!", e);
			result = ERROR;
		}
		ModelMap modelMap = new ModelMap();
		modelMap.put("result", result);
		modelMap.put("data", pbTypeList);
		return modelMap;
	}
}